package net.esj.basic.utils.qrcode;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import com.google.zxing.ZxingEncoderHandler;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class QrCodeUtils {
	
	private static final int CROSS_COUNT = 7;
	private static final int LENGTH_COUNT  = 10;
	private static final int LR_REMAIN = 85;
	private static final int TOP_REMAIN = 200;
	
	private static final int PAPER_WIDTH = 2970;
	private static final int PAPER_HIGH = 4200;
	
	
	/**
	 * 创建ZIP文件
	 * @param outputStream
	 * @param codes 二维码表示的值
	 * @param keys 二维码下方显示的值
	 * @throws IOException 
	 */
	public static void createInZip(OutputStream outputStream,String[] codes,String[] keys) throws IOException{
		ZipOutputStream zos = new ZipOutputStream(outputStream);
		List<QrCodeImage> images  = createImages(codes, keys);
		for(QrCodeImage image : images){
			zos.putNextEntry(new ZipEntry(image.getName()));
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(zos);
			JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image.getImage());
			param.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH);
			param.setXDensity(300);
			param.setYDensity(300);
			encoder.encode(image.getImage(),param);
			zos.closeEntry();
		}
		zos.close();
	}
	
	private static final List<QrCodeImage> createImages(String[] codes,String[] keys){
		int sum = CROSS_COUNT * LENGTH_COUNT;
		int c = 0;
		
		List<QrCodeImage> retal = new ArrayList<QrCodeImage>();
		
		BufferedImage mainImg = null;
		Graphics2D g = null;
		int typeCount = 1;
		
		
		for(int i=0;i<codes.length;i++){
			String code = codes[i];
			String key = keys[i];
				int hang = c%CROSS_COUNT;
				int lie = c/CROSS_COUNT;
				if(c==0){
					mainImg = new BufferedImage(PAPER_WIDTH, PAPER_HIGH,BufferedImage.TYPE_INT_RGB);
					g = mainImg.createGraphics(); 
					g.fillRect(0, 0, PAPER_WIDTH, PAPER_HIGH);
					g.setColor(Color.BLACK);
					/*g.setFont(new Font("Times New Roman", Font.BOLD, 75));
					g.drawString(serials[m], 1300, 100);*/
					for(int j=0;j<LENGTH_COUNT;j++){
						drawDottedLine(g,Color.BLACK, 0, TOP_REMAIN+j*400, PAPER_WIDTH, TOP_REMAIN+j*400);//横向10根线
					}
					for(int j=0;j<(CROSS_COUNT+1);j++){
						drawDottedLine(g,Color.BLACK, LR_REMAIN+j*400, TOP_REMAIN, LR_REMAIN+j*400, PAPER_HIGH);//纵向8根线
					}
				}
				BufferedImage img = (BufferedImage) ZxingEncoderHandler.encodeToImage(code, 300, 300);
				g.drawImage(img, hang*400+LR_REMAIN+50, lie*400+TOP_REMAIN+20, null);//画二维码
				g.setColor(Color.BLACK);
				g.setFont(new Font("Times New Roman", Font.BOLD, 20));
				//g.drawString(ss, hang*400+LR_REMAIN+100-(ss.length()*5), lie*400+TOP_REMAIN+325);
				drawString(g, key, hang, lie);
				
				c++;
				if(c==sum){//满了输出
					QrCodeImage qi = new QrCodeImage(mainImg, typeCount+".JPEG");
					retal.add(qi);
					c=0;
					typeCount++;
				}
		}
		
		if(c>0){
			QrCodeImage qi = new QrCodeImage(mainImg, typeCount+".JPEG");
			retal.add(qi);
		}
		return retal;
	}
	
	private static final void drawDottedLine(Graphics2D g2,Color color,int x1,int y1,int x2,int y2){
		Stroke sk = g2.getStroke();
		Color col = g2.getColor();
		Stroke newStroke = new BasicStroke(1,BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0,new float[]{16,4},0);
		g2.setStroke(newStroke);
		g2.setColor(color);
		g2.drawLine(x1, y1, x2,y2);
		g2.setStroke(sk);
		g2.setColor(col);
	}
	
	private static final void drawString(Graphics2D g2,String ss,int hang,int lie){
		int tw = g2.getFontMetrics().bytesWidth(ss.getBytes(), 0, ss.getBytes().length);
		String[] paintStr = new String[(tw+399)/400];
		
		int perlen = (ss.length()+paintStr.length-1)/paintStr.length;
		
		for(int i=0;i<paintStr.length;i++){
			if(i!=(paintStr.length-1)){
				paintStr[i] = ss.substring(perlen*i, perlen*(i+1)-1);
			}else{
				paintStr[i]  = ss.substring(perlen*i);
			}
		}
		for(int i=0;i<paintStr.length;i++){
			String str = paintStr[i];
			int twidth = g2.getFontMetrics().bytesWidth(str.getBytes(), 0, str.getBytes().length);//文字宽度
			int cen = hang*400+LR_REMAIN+200;//居中值
			int dx = cen-twidth/2;
			g2.drawString(str, dx, lie*400+TOP_REMAIN+325+20*i);
		}
	}
}
